package com.hiultra.archive.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.hiultra.archive.domain.ArcBox;
import com.hiultra.archive.domain.ArchiveBoxMove;
import com.hiultra.archive.domain.ArchiveBoxMoveTask;
import com.hiultra.archive.domain.ArchiveBoxStatus;
import com.hiultra.archive.mapper.ArcBoxMapper;
import com.hiultra.archive.mapper.ArchiveBoxMoveMapper;
import com.hiultra.archive.mapper.ArchiveBoxMoveTaskMapper;
import com.hiultra.archive.mapper.ArchiveBoxStatusMapper;
import com.hiultra.archive.service.IArchiveBoxMoveService;
import com.hiultra.common.constant.BusinessConstants;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.exception.ServiceException;
import com.hiultra.common.utils.DateUtils;
import com.hiultra.common.utils.SecurityUtils;
import com.hiultra.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 盒移位Service业务层处理
 *
 * @author 派大星
 * @date 2024-07-23
 */
@Service
public class ArchiveBoxMoveServiceImpl implements IArchiveBoxMoveService {
    @Autowired
    private ArchiveBoxMoveMapper archiveBoxMoveMapper;
    @Autowired
    private ArchiveBoxMoveTaskMapper archiveBoxMoveTaskMapper;
    @Autowired
    private ArcBoxMapper arcBoxMapper;
    @Autowired
    private ArchiveBoxStatusMapper archiveBoxStatusMapper;

    /**
     * 查询盒移位
     *
     * @param id 盒移位主键
     * @return 盒移位
     */
    @Override
    public ArchiveBoxMove selectArchiveBoxMoveById(String id) {
        return archiveBoxMoveMapper.selectArchiveBoxMoveById(id);
    }

    /**
     * 查询盒移位列表
     *
     * @param archiveBoxMove 盒移位
     * @return 盒移位
     */
    @Override
    public List<ArchiveBoxMove> selectArchiveBoxMoveList(ArchiveBoxMove archiveBoxMove) {
        return archiveBoxMoveMapper.selectArchiveBoxMoveList(archiveBoxMove);
    }

    /**
     * 新增盒移位
     *
     * @param archiveBoxMove 盒移位
     * @return 结果
     */
    @Override
    public int insertArchiveBoxMove(ArchiveBoxMove archiveBoxMove) {
        return archiveBoxMoveMapper.insertArchiveBoxMove(archiveBoxMove);
    }

    /**
     * 修改盒移位
     *
     * @param archiveBoxMove 盒移位
     * @return 结果
     */
    @Override
    public int updateArchiveBoxMove(ArchiveBoxMove archiveBoxMove) {
        return archiveBoxMoveMapper.updateArchiveBoxMove(archiveBoxMove);
    }

    /**
     * 批量修改盒移位
     *
     * @param archiveBoxMoveList 盒移位
     * @return
     */
    @Override
    public int updateArchiveBoxMoveList(List<ArchiveBoxMove> archiveBoxMoveList, String userName) {
        for (ArchiveBoxMove archiveBoxMove : archiveBoxMoveList) {
            //记录操作人和操作时间   子表位置
            //todo 主表状态
            ArchiveBoxMoveTask archiveBoxMoveTask = archiveBoxMoveTaskMapper.selectArchiveBoxMoveTaskByTaskId(archiveBoxMove.getTaskId());
            archiveBoxMoveTask.setModifier(userName);
            archiveBoxMoveTask.setModifyTime(new Date());

            ArcBox arcBox = arcBoxMapper.selectArcBoxByBoxCode(archiveBoxMove.getBoxNo());
            arcBox.setHouseLocationNo(archiveBoxMove.getNewHouseLocationNo());

            arcBoxMapper.updateArcBox(arcBox);
            archiveBoxMoveMapper.updateArchiveBoxMove(archiveBoxMove);
            archiveBoxMoveTaskMapper.updateArchiveBoxMoveTask(archiveBoxMoveTask);
        }
        return 1;
    }

    /**
     * 移位完成
     *
     * @param archiveBoxMoveList
     * @return
     */
    @Override
    @Transactional
    public AjaxResult endArchiveBoxMoveList(List<ArchiveBoxMove> archiveBoxMoveList) {
        if (archiveBoxMoveList.size() < 1) {
            throw new ServiceException("传入的明细列表为空,请修改后重试！");
        }
        for (ArchiveBoxMove archiveBoxMove : archiveBoxMoveList) {
            if (StringUtils.isNotNull(archiveBoxMove)
                    && (ObjectUtil.isEmpty(archiveBoxMove.getNewHouseLocationNo()))) {
                return AjaxResult.error("序号为" + archiveBoxMove.getId() + "的新库位号为空,请修改后重试！");
            }
            //修改档案库位号
            ArcBox arcBox = arcBoxMapper.selectArcBoxByBoxCode(archiveBoxMove.getBoxNo());
            //判空
            if (ObjectUtil.isEmpty(arcBox)) {
                return AjaxResult.error("序号为" + archiveBoxMove.getId() + "的档案盒不存在,请修改后重试！");
            }

            // 新库位号的结构是01区01柜01节02层
            String newLocationCode = archiveBoxMove.getNewHouseLocationNo();

            // 检查新库位号的长度是否符合规范
            if (newLocationCode.length() < 8) {
                // 处理长度不符合规范的情况，比如抛出异常或者给出默认值
                // 这里只是一个简单的示例，具体处理方式根据业务需求来定
                throw new ServiceException("新库位号长度不符合规范");
            } else {
                // 从存放位置中截取前缀部分
                int prefixIndex = arcBox.getStorLoc().indexOf("室") + 1;
                String prefix = arcBox.getStorLoc().substring(0, prefixIndex);

                // 更新档案盒相关字段
                arcBox.setHouseLocationNo(newLocationCode);
                arcBox.setAreaNo(newLocationCode.substring(0, 2)); // 档案区编号
                arcBox.setCabinetNo(newLocationCode.substring(2, 4)); // 档案柜编号
                arcBox.setNodesNo(newLocationCode.substring(4, 6)); // 节编号
                arcBox.setLayerNo(newLocationCode.substring(6, 8)); // 层号
                arcBox.setLocNo(newLocationCode.substring(8)); // 位号
                arcBox.setStorLoc(prefix + newLocationCode.substring(0, 2) + "区" + newLocationCode.substring(2, 4) + "柜" + newLocationCode.substring(4, 6) + "节" + newLocationCode.substring(6, 8) + "层" + newLocationCode.substring(8) + "位");
            }

            arcBoxMapper.updateArcBox(arcBox);

            //记录档案盒状态
            ArchiveBoxStatus archiveBoxStatus = new ArchiveBoxStatus();
            archiveBoxStatus.setAreaNo(arcBox.getAreaNo());
            archiveBoxStatus.setCabinetNo(arcBox.getCabinetNo());
            archiveBoxStatus.setBoxBarCode(arcBox.getBoxRfid());
            archiveBoxStatus.setBoxNo(arcBox.getBoxCode());
            archiveBoxStatus.setHouseLocationNo(arcBox.getHouseLocationNo());

            archiveBoxStatus.setBoxStatus(BusinessConstants.BOX_MOVE);
            archiveBoxStatus.setRecordTime(DateUtils.getNowDate());
            archiveBoxStatus.setPushStatus(BusinessConstants.PUSHSTATUS);

            archiveBoxStatusMapper.insertArchiveBoxStatus(archiveBoxStatus);

        }
        ArchiveBoxMove archiveBoxMove = archiveBoxMoveList.get(0);
        ArchiveBoxMoveTask archiveBoxMoveTask = archiveBoxMoveTaskMapper.selectArchiveBoxMoveTaskByTaskId(archiveBoxMove.getTaskId());
        archiveBoxMoveTask.setModifier(SecurityUtils.getLoginUser().getUser().getCustNo());
        archiveBoxMoveTask.setModifyTime(DateUtils.getNowDate());
        archiveBoxMoveTask.setBoxMoveStatus(BusinessConstants.END_BOX_MOVE_STATUS);
        archiveBoxMoveTaskMapper.updateArchiveBoxMoveTask(archiveBoxMoveTask);

        return AjaxResult.success();
    }

    /**
     * 批量删除盒移位
     *
     * @param ids 需要删除的盒移位主键
     * @return 结果
     */
    @Override
    public int deleteArchiveBoxMoveByIds(String[] ids) {
        return archiveBoxMoveMapper.deleteArchiveBoxMoveByIds(ids);
    }

    /**
     * 删除盒移位信息
     *
     * @param id 盒移位主键
     * @return 结果
     */
    @Override
    public int deleteArchiveBoxMoveById(String id) {
        return archiveBoxMoveMapper.deleteArchiveBoxMoveById(id);
    }


    /**
     * 根据任务id查询盒移位信息详情
     *
     * @param taskId
     * @return
     */
    @Override
    public List<ArchiveBoxMove> selectArchiveBoxMoveByTaskId(String taskId) {
        return archiveBoxMoveMapper.selectArchiveBoxMoveByTaskId(taskId);
    }


}
